﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;

namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class DrugInfoEdit : BasePage
    {
        IBLL_Goods goodCtrl = Factory.BLL_Goods();
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取需要新增调价计划的采购产品的信息
            if (!this.IsPostBack)
            {
                if (Request["goodsid"] == null)
                {
                    PageError("参数错误！", this.ReturnUrl);
                    return;
                }
                string goodId = Request.QueryString["goodsid"].ToString();
                Model.View.Goods goodModel = goodCtrl.GetGoodsByID(goodId);
                if (goodModel != null)
                {
                    if (goodModel.Source >= 2)
                    {
                        txtCompany_SC.Text = goodModel.CompanyName_SC;
                        txtGoodsName.Text = goodModel.GoodsName;
                        txtOutLook.Text = goodModel.Outlookc;
                        this.ddlSortSource.SelectedValue = goodModel.Source.Value.ToString();
                        txtAdjustPrice.Text = (goodModel.LimitPrice != null)? goodModel.LimitPrice.Value.ToString("F2"):"0.00";
                        txtGoodsName.Text = goodModel.GoodsName;
                        txtUnit.Text = goodModel.Unit;
                        txtMaterial.Text = goodModel.Material;
                    }
                    else
                    {
                        PageError("产品为中标药，不允许修改！", this.ReturnUrl);
                    }

                }
                else
                {
                    PageError("采购产品不存在！", this.ReturnUrl);
                }
            }
        }

        /// <summary>
        /// 增加价格调整计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (string.IsNullOrEmpty(txtAdjustPrice.Text.Trim()))
                {
                    PageError("新价格不能为空！", "");
                    return;
                }
                Model.Goods goodModel = new Model.Goods()
                    {
                        GoodsID = Request["goodsid"],
                        LimitPrice =new HZ.Common.ConvertHelper(txtAdjustPrice.Text.Trim()).ToDecimal,
                        Source = new HZ.Common.ConvertHelper( ddlSortSource.SelectedValue ).ToInt32
                    };

                int result = goodCtrl.UpdateGoods( goodModel );
                if (result == 1)
                {
                    PageInfo("修改成功！", this.ReturnUrl);
                }
                else
                {
                    PageError("修改失败！", "");
                }

            }
        }

    }
}
